package main

import (
	"fmt"
)

func lengthOfLongestSubstring(s string) int {
	// 记录下标的hash表
	m := map[byte]int{}
	// 字符串的长度
	n := len(s)
	// 窗口的右指针
	rk, ans := -1, 0
	for i := 0; i < n; i++ {
		if i != 0 {
			delete(m, s[i-1])
		}
		for rk+1 < n && m[s[rk+1]] == 0 {
			m[s[rk+1]]++
			rk++
		}
		ans = max(ans, rk-i+1)
	}
	return ans
}

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func main() {
	var s1 = "bbbbb"
	var s2 = "abcabca"
	strLen1 := lengthOfLongestSubstring(s1)
	fmt.Println(strLen1)
	strLen2 := lengthOfLongestSubstring(s2)
	fmt.Println(strLen2)

}
